<?php
/*Loading view, model*/
@(include_once("model.php")) OR die("Error. File not found - 'model.php'");
@(include_once("view.php")) OR die("Error. File not found - 'view.php'");

@(include_once(real_path ."kse". DIRSEP ."core". DIRSEP ."dicts". DIRSEP . kselanguage ."_dict.php")) OR die ("Error. File not found - '". real_path ."kse". DIRSEP ."core". DIRSEP ."dicts". DIRSEP . kselanguage ."_dict.php'");
@(include_once(real_path ."kse/functions.php")) OR die ("Error. File not found - '". real_path ."kse/functions.php");


function parse($route){
	$conn = mysql_connect(DB_HOST, DB_USER, DB_PASS) or die ('Error connecting to mysql');
	mysql_select_db(DB_NAME);
	$query = "SET NAMES utf8";
	$makeresult = mysql_query($query) or die(mysql_error() ."<br>". $query);
	/*check if page exists*/
	$result = checkPath($route);

	if(!$result){
		return displayError(404, $route);//return error
	}
	else{
		list($page_title, $page_keywords, $page_description, $page_content, $page_template_id, $page_locked) = mysql_fetch_row($result);	
		
		/*Check if using modules*/
		$query = "SELECT SQL_NO_CACHE t2.module_filesystem_name FROM kse_site_template_module AS t1 LEFT JOIN kse_modules AS t2 ON t1.tm_module_id = t2.module_id  WHERE t1.tm_template_id=". $page_template_id .";";
		$result = mysql_query($query);

		if(mysql_num_rows($result) > 0){//site using modules
			//include model and give control to this module
			list($module_filesystem_name) = mysql_fetch_row($result);	
			include_once(real_path ."kse". DIRSEP ."modules". DIRSEP . $module_filesystem_name . DIRSEP ."model.php");
			include_once(real_path ."kse". DIRSEP ."modules". DIRSEP . $module_filesystem_name . DIRSEP ."cfg.php");
			useModel($page_title, $page_keywords, $page_description, $page_content, $page_template_id, $page_locked);
		}
		else{
			return displayTemplated($page_title, $page_keywords, $page_description, $page_content, $page_template_id, $page_locked);//return site
		}
	}
	mysql_close($conn);
}

function checkPath($route){
	$query = "SELECT page_title, page_keywords, page_description, page_content, page_template_id, page_locked FROM kse_site_pages WHERE page_filesystem_name='". $route ."'";
	$result = mysql_query($query);
	if(mysql_num_rows($result) > 0){
		return $result;
	}
	else{
		return false;
	}
}
?>